var layer = {
    flyToViewPoint: function (position, direction, up) {
        //飞行到某一个点
        viewer.camera.flyTo({
            destination: new LSGlobe.Cartesian3(position.x, position.y, position.z),
            orientation: {
                direction: new LSGlobe.Cartesian3(direction.x, direction.y, direction.z),
                up: new LSGlobe.Cartesian3(up.x, up.y, up.z)
            }
        });
    },
    getFlyViewPos: function (xGuid) {
        var datas = {};
        datas.guid = xGuid;
        var url = basePath + "/TX_SceneManager/vp/queryViewPointByGuid.action";
        $.ajax({
            url: url,
            type: "post",
            data: datas,
            async: false,
            dataType: "Json",
            success: function (result) {
                if (result.resultCode == "1") {
                    var oPosition = {};
                    oPosition.x = result.viewpoint.posx;
                    oPosition.y = result.viewpoint.posy;
                    oPosition.z = result.viewpoint.posz;

                    var oUp = {};
                    oUp.x = result.viewpoint.upx;
                    oUp.y = result.viewpoint.upy;
                    oUp.z = result.viewpoint.upz;

                    var oDirection = {};
                    oDirection.x = result.viewpoint.directionx;
                    oDirection.y = result.viewpoint.directiony;
                    oDirection.z = result.viewpoint.directionz;

                    flyToViewPoint(oPosition, oDirection, oUp);
                } else {
                    console.error(result.resultMsg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "获取默认视角失败" });
            }
        });
    },
    FLyToSceneDefault: function (sPos) {
        viewer.camera.flyTo({
            destination: new LSGlobe.Cartesian3(sPos.position.x, sPos.position.y, sPos.position.z),
            orientation: {
                direction: new LSGlobe.Cartesian3(sPos.direction.x, sPos.direction.y, sPos.direction.z),
                up: new LSGlobe.Cartesian3(sPos.up.x, sPos.up.y, sPos.up.z)
            },
            duration: 2
        });
    },
	 FlyToByDegree:function(opos){
    //飞到特定点byDegree
		viewer.camera.flyTo({
	       	destination : LSGlobe.Cartesian3.fromDegrees(opos.x,opos.y,opos.z+1500),
	       	duration:3
	   	}); 
	},
    init: function () {
        /********************************** 图层管理  ***********************************/
        var SceneDefaultPos = {
            "position": { "x": "-2043279.0519806324", "y": "9734417.53494849", "z": "7521149.159344427" },
            "up": { "x": "0.30428262817203067", "y": "-0.5811819872180399", "z": "0.7547447117575663" },
            "direction": { "x": "0.14932425978082173", "y": "-0.7534180942517841", "z": "-0.640361960687016" }
        }

        //左边图片加载scrollbar
        $(".analy-body").mCustomScrollbar();
        //窗口变化
        $(window).resize(function () {
            $(".analy-body").mCustomScrollbar("update");
        });

        $(document).on("click", "#sceneBox .layer-item-title", function () {
            var oThis = $(this);
            var serType = oThis.attr("type");
            var oGuid = oThis.attr("guid");
            //飞行
            layer.FLyToSceneDefault(SceneDefaultPos);
            setTimeout(function () {
                if (serType == "1") {
                    layer.FlyToByDegree(aTileset[oGuid]._position);
                } else if (serType == "2") {
                    layer.FlyToByDegree(aObj[oGuid]._position);
                } else if (serType == "3") {
                    if (!!aDatasource[oGuid]) {
                        viewer.flyTo(aDatasource[oGuid], { duration: 3 });
                    } else {
                        viewer.flyTo(getDatasourceById(oGuid));
                        aDatasource[oGuid] = getDatasourceById(oGuid);
                    }
                    //viewer.flyTo(getDatasourceById(oGuid),{duration:3});
                } else if (serType == "4") {
                    var rect = aLayer[oGuid]._imageryProvider._rectangle;
                    viewer.camera.flyTo({
                        destination: rect
                    }, { duration: 3 });
                }
            }, 2000)
        });

        //显示隐藏 
        $(document).on("click", "#analysis-panel .layer-item.layers .checkicon", function () {
            var oThis = $(this);
            if (oThis.hasClass("active")) {
                oThis.removeClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    var oChildChange = oThis.parent().next().find(".checkicon").removeClass("active");
                    for (var a = 0; a < oChilds.length; a++) {
                        var sGuid = oChilds.eq(a).parent().attr("guid");
                        var sType = oChilds.eq(a).parent().attr("type");
                        if (sType == "3") {
                            getDatasourceById(sGuid).show = false;
                        } else if (sType == "4") {
                            aLayer[sGuid].show = false;
                        } else {
                            getPrimitiveById(sGuid).show = false;
                        }
                    }
                } else {
                    var oParentChild = oThis.parents(".layer-item-content").children().length;
                    var oParentBox = oThis.parents(".layer-item-content").find(".checkicon.active");
                    if (oParentBox.length <= 0) {
                        oThis.parents(".layer-item-content").prev().find(".checkicon").removeClass("active");
                    }
                    var oGuid = oThis.parent().attr("guid");
                    var oType = oThis.parent().attr("type");
                    if (oType == "3") {
                        getDatasourceById(oGuid).show = false;
                    } else if (oType == "4") {
                        aLayer[oGuid].show = false;
                    } else {
                        getPrimitiveById(oGuid).show = false;
                    }
                }
            } else {
                oThis.addClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    var oChildChange = oThis.parent().next().find(".checkicon").addClass("active");
                    for (var a = 0; a < oChilds.length; a++) {
                        var sGuid = oChilds.eq(a).parent().attr("guid");
                        var sType = oChilds.eq(a).parent().attr("type");
                        if (sType == "3") {
                            getDatasourceById(sGuid).show = true;
                        } else if (sType == "4") {
                            aLayer[sGuid].show = true;
                        } else if (sType == "1") {
                            getPrimitiveById(sGuid).show = true;
                        } else {
                            var opacity = $("#setting-container .EarthAp").val();
                            var fModel = getPrimitiveById(sGuid);
                            console.log(fModel);
                            if (opacity == 0) {
                                if (fModel.name.underground == "2") {
                                    fModel.show = true;
                                }
                            } else if (opacity == 100) {
                                if (fModel.name.underground == "1") {
                                    fModel.show = true;
                                }
                            } else {
                                fModel.show = true;
                            }
                        }
                    }
                } else {
                    var oParentChild = oThis.parents(".layer-item-content").children().length;
                    var oParentBox = oThis.parents(".layer-item-content").find(".checkicon.active");
                    if (oParentChild == oParentBox.length) {
                        oThis.parents(".layer-item-content").prev().find(".checkicon").addClass("active");
                    }
                    var oGuid = oThis.parent().attr("guid");
                    var oType = oThis.parent().attr("type");
                    if (oType == "3") {
                        getDatasourceById(oGuid).show = true;
                    } else if (oType == "4") {
                        aLayer[oGuid].show = true;
                    } else if (oType == "1") {
                        getPrimitiveById(oGuid).show = true;
                    } else {
                        var opacity = $("#setting-container .EarthAp").val();
                        var fModel = getPrimitiveById(oGuid);
                        console.log(fModel);
                        if (opacity == 0) {
                            if (fModel.name.underground == "2") {
                                fModel.show = true;
                            }
                        } else if (opacity == 100) {
                            if (fModel.name.underground == "1") {
                                fModel.show = true;
                            }
                        } else {
                            fModel.show = true;
                        }
                    }
                }
            }
            return false;
        });


        //地形图层的显示隐藏
        //setTerrain(parm)
        $(document).on("click", "#analysis-panel .layer-item.terrain .checkicon", function () {
            var oThis = $(this);
            if (oThis.hasClass("active")) {
                oThis.removeClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    oChilds.removeClass("active");
                } else {
                    var oChilds = oThis.parents(".layer-item-content").prev().find(".checkicon");
                    oChilds.removeClass("active");
                    setTerrain("none");
                }
                setTerrain("none");
            } else {
                oThis.addClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    oChilds.addClass("active");
                } else {
                    var oChilds = oThis.parents(".layer-item-content").prev().find(".checkicon");
                    oChilds.addClass("active");
                }
                setTerrain("stk");
            }
        });

        //树形菜单每个节点关闭显示
        $(document).on("click", "#analysis-panel .arrow", function () {
            var oThis = $(this);
            if (oThis.hasClass("active")) {
                oThis.removeClass("active");
                oThis.parent().next().show();
            } else {
                oThis.addClass("active");
                oThis.parent().next().hide();
            }
        });
    }
}